<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>惠农网 - 商品详情</title>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css" />
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/css/my.css" />
</head>
<body>
<div class="container">
    <%@include file="common/header.jsp"%>
    <div class="row upDown">
        <%--左侧--%>
        <div class="col-2">
            <%@include file="common/product-class.jsp"%>
            <%@include file="common/last-view.jsp"%>
        </div>
        <%--中部--%>
        <div class="col-10">
            <%--展示商品详情--%>
            <div class="row w-100">
                <div class="card mb-3 w-100" >
                    <div class="row no-gutters">
                        <div class="col-md-4">
                            <img src="${pageContext.request.contextPath}/images/product/${ebProduct.epFileName}" class="card-img" alt="${ebProduct.epName}">
                        </div>
                        <div class="col-md-8">
                            <div class="card-body">
                                <h5 class="card-title">${ebProduct.epName}</h5>
                                <p class="card-text">${ebProduct.epDescription} </p>
                                <c:if test="${ebProduct.epStock!=0}">
                                    <p class="card-text"><small class="text-muted">库　存：有货(库存${ebProduct.epStock}件)</small></p>
                                </c:if>
                                <c:if test="${ebProduct.epStock==0}">
                                    <p class="card-text"><small class="text-muted">库　存：无货</small></p>
                                </c:if>
                                <p class="card-text"><small class="text-muted">￥${ebProduct.epPrice}</small></p>
                                <a href="javascript:add();" class="btn btn-sm btn-primary">加入购物车</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row text-center">
        <%@include file="common/footer.jsp"%>
    </div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/popper.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/bootstrap.min.js"></script>
</body>
<script>
    console.log("执行3");
    (function (){
        const lastProduct = {epId:${ebProduct.epId},epName:"${ebProduct.epName}",epFileName:"${ebProduct.epFileName}"};
        const lastList = localStorage.getItem("lastList");
        if(lastList!=null){
            const list = JSON.parse(lastList);
            list.push(lastProduct);
            if(list.length>5){
                list.shift();
            }
            localStorage.setItem("lastList", JSON.stringify(list));
            console.log("执行")
        }else {
            const list = new Array();
            list.push(lastProduct);
            localStorage.setItem("lastList", JSON.stringify(list));
            console.log("执行");
        }
    })();
    function add(){
        const product = {epId:${ebProduct.epId},num:1};
        const addcar = localStorage.getItem("addcar");
        if(addcar!=null){
            const list = JSON.parse(addcar);
            let flag = 0;
            for (let i = 0;i < list.length;++i){
                if (list[i].epId==product.epId){
                    list[i].num++;
                    flag = 1;
                    break;
                }
            }
            if (flag != 1){
                list.push(product);
            }
            localStorage.setItem("addcar", JSON.stringify(list));
        }else {
            const list = new Array();
            list.push(product);
            localStorage.setItem("addcar", JSON.stringify(list));
        }
        // const addcar1 = localStorage.getItem("addcar");
        // console.log(addcar1);
        alert('添加成功')
    }
</script>
</html>
